#include "OLED.h"

#define OLED_ADDRESS 0x78
uint8_t GRAM[8][128];//OLED像素点

void OLED_SendCmd(uint8_t cmd)//发送数据(写数据)
{
	uint8_t sendbuffer[2];
	sendbuffer[0] = 0x00;
	sendbuffer[1] = cmd;
	HAL_I2C_Master_Transmit(&hi2c1,OLED_ADDRESS,sendbuffer,2,HAL_MAX_DELAY);
}

void OLED_Clear()//清屏
{
	OLED_NewFrame();
	OLED_ShowFrame();
}
void OLED_NewFrame()//清空显存
{
	memset(GRAM,0,sizeof(GRAM));//向GRAM中填充数据0
}

void OLED_ShowFrame()//将显存显示到屏幕
{
	uint8_t sendbuffer[129];
	sendbuffer[0] = 0x40;//发送的是显示在屏幕的数据
	for(uint8_t i = 0;i < 8;i++)//第0页到第7页
	{
		for(uint8_t j = 0;j < 128;j++)//第0列到第127列
		{
			sendbuffer[j+1] = GRAM[i][j];
		}
		OLED_SendCmd(0xB0 + i);//设置页地址
		OLED_SendCmd(0x00);
		OLED_SendCmd(0x10);
		HAL_I2C_Master_Transmit(&hi2c1,OLED_ADDRESS,sendbuffer,sizeof(sendbuffer),HAL_MAX_DELAY);
	}
}


/**
  * @brief  OLED显示一个字符
  * @param  Line 行位置，范围：1~4
  * @param  Column 列位置，范围：1~8
  * @param  Char 要显示的一个字符，范围：ASCII可见字符
  * @retval 无
  */
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{      	
	
	uint8_t i = 0;
	uint8_t tmpColumn = (Column - 1)*16;
	uint8_t tmpLine = (Line-1)*2;
	for(uint8_t y = 0;y < 2;y++)
	{
		for(uint8_t x = tmpColumn;x < tmpColumn + 16;x++)
		{
			GRAM[tmpLine + y][x]=OLED_ASCII[Char - ' '][i++];
		}
	}
	OLED_ShowFrame();
}
/**
  * @brief  OLED显示一串字符型数字
  * @param  Line 行位置，范围：1~4
  * @param  Column 列位置，范围：1~8
  * @param  Char 要显示的一个字符，范围：ASCII可见字符
  * @retval 无
  */
void OLED_ShowString(uint8_t Line, uint8_t Column, char* Str)
{
	
	uint8_t i = 0;
	uint8_t tmpColumn = (Column - 1)*16;
	uint8_t tmpLine = (Line-1)*2;
	while(Str[i] != '\0')
	{
		uint8_t j = 0;
		for(uint8_t y = 0;y < 2;y++)
		{
			for(uint8_t x =  tmpColumn;x <  tmpColumn + 16;x++)
			{
				GRAM[tmpLine + y][x]=OLED_ASCII[Str[i] - ' '][j++];
			}
		}
		tmpColumn+=8;
		i++;
	}
	OLED_ShowFrame();
}

void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number)
{
	/********转换整数部分为字符串********/
	char intStr[10] = {'\0'};
	char tmp[10] = {'\0'};
	char idx = 0;//intStr[]的指针
	if (Number == 0)//整数部分为0
	{
		intStr[idx++] = '0';
	}
	else//整数部分不为0
	{
		while (Number != 0)
		{
			intStr[idx++] = Number % 10 + '0';
			Number = Number / 10;
		}
	}
	/***************************************/
	/*********反转整数部分字符串*********/
	char i = 0;
	while (idx > 0)
	{
		tmp[i++] = intStr[idx - 1];
		idx--;
	}
	/***************************************/
	OLED_ShowString(Line,Column,tmp);
}

void OLED_Init()//初始化
{
	uint32_t i, j;
	
	for (i = 0; i < 1000; i++)			//上电延时
	{
		for (j = 0; j < 1000; j++);
	}
	OLED_SendCmd(0xAE);	//关闭显示
	
	OLED_SendCmd(0xD5);	//设置显示时钟分频比/振荡器频率
	OLED_SendCmd(0x80);
	
	OLED_SendCmd(0xA8);	//设置多路复用率
	OLED_SendCmd(0x3F);
	
	OLED_SendCmd(0xD3);	//设置显示偏移
	OLED_SendCmd(0x00);
	
	OLED_SendCmd(0x40);	//设置显示开始行
	
	OLED_SendCmd(0xA1);	//设置左右方向，0xA1正常 0xA0左右反置
	
	OLED_SendCmd(0xC8);	//设置上下方向，0xC8正常 0xC0上下反置

	OLED_SendCmd(0xDA);	//设置COM引脚硬件配置
	OLED_SendCmd(0x12);
	
	OLED_SendCmd(0x81);	//设置对比度控制
	OLED_SendCmd(0xCF);

	OLED_SendCmd(0xD9);	//设置预充电周期
	OLED_SendCmd(0xF1);

	OLED_SendCmd(0xDB);	//设置VCOMH取消选择级别
	OLED_SendCmd(0x30);

	OLED_SendCmd(0xA4);	//设置整个显示打开/关闭

	OLED_SendCmd(0xA6);	//设置正常/倒转显示

	OLED_SendCmd(0x8D);	//设置充电泵
	OLED_SendCmd(0x14);

	OLED_SendCmd(0xAF);	//开启显示
	
	OLED_Clear();
}

